﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFramework;

namespace WpfElevator.ControlPart
{
    class ControllerImpl1 : AController        
    {
        public override void addOutsideEvent(AbstractFramework.LogicControl.IEvent inputevent)
        {
            System.Console.WriteLine("into addOutsideEvent :inputevent.eventType=" + inputevent.eventType + "inputevent.floor= " + inputevent.floor + "inputevent.updown=" + inputevent.updown + "\r\n");
            //inputevent.requisitionFloor;inputevent.updown;inputevent.floor
            int[] reDes = new int[elevator.Length];//此数组存放每个电梯到请求楼层的相对距离
            //计算每个电梯到请求楼层 的相对距离
            System.Console.WriteLine("elevator.Length=" + elevator.Length);

            int flag = 0;

            for (int i = 0; i < elevator.Length; i++)
            {  //遍历每个电梯

                if (elevator[i].FbdFloor.Contains(inputevent.floor + 1))
                {
                    reDes[i] = int.MaxValue;
                    continue;
                }

                else
                    flag = 1;

                if (elevator[i].State == MovementStatus.Stoped)
                {
                    reDes[i] = System.Math.Abs(inputevent.floor - elevator[i].Floor);
                    continue;
                }

                int Max_floor = taskList[i].MaxFloors();
                int Min_floor = taskList[i].MinFloors();
                Max_floor = Max_floor > inputevent.floor ? Max_floor : inputevent.floor;
                Min_floor = Min_floor < inputevent.floor ? Min_floor : inputevent.floor;

                if (elevator[i].State == MovementStatus.UpMoving && inputevent.floor >= elevator[i].Floor)
                {
                    //如果电梯上行，且请求楼层高于电梯所在楼层

                    if (inputevent.updown == 1)//如果请求是向上的
                        reDes[i] = inputevent.floor - elevator[i].Floor;//距离为请求楼层-电梯楼层
                    else
                        reDes[i] = 2 * Max_floor - elevator[i].Floor - inputevent.floor;//如果请求是向下的，距离为电梯要到的最高楼层*2-请求楼层-电梯楼层
                }
                else if (elevator[i].State == MovementStatus.UpMoving && inputevent.floor < elevator[i].Floor)
                {
                    //  如果电梯上行，且请求楼层低于电梯所在楼层       
                    if (inputevent.updown == 1)//如果请求是向上的
                        reDes[i] = 2 * Max_floor - 2 * Min_floor - elevator[i].Floor + inputevent.floor;
                    else
                        reDes[i] = 2 * Max_floor - elevator[i].Floor - inputevent.floor;
                }
                else if (elevator[i].State == MovementStatus.DownMoving && inputevent.floor >= elevator[i].Floor)
                {
                    //如果电梯下行，且请求楼层高于电梯所在楼层

                    if (inputevent.updown == 1)//如果请求是向上的
                        reDes[i] = elevator[i].Floor + inputevent.floor - 2 * Min_floor;
                    else
                        reDes[i] = 2 * Max_floor - 2 * Min_floor + elevator[i].Floor - inputevent.floor;
                }
                else if (elevator[i].State == MovementStatus.DownMoving && inputevent.floor < elevator[i].Floor)
                {
                    //如果电梯下行，且请求楼层高于电梯所在楼层
                    if (inputevent.updown == 1)//如果请求是向上的
                        reDes[i] = elevator[i].Floor + inputevent.floor - 2 * Min_floor;
                    else
                        reDes[i] = elevator[i].Floor - inputevent.floor;

                }
                else
                    ;

            }
            if (flag == 0)
                return;

            for (int i = 0; i < elevator.Length; i++)
                System.Console.WriteLine("reDes[i]=" + reDes[i] + " i =" + i);
            //找出相对距离最短的电梯下标

            int tempMin = reDes.Min();
            System.Console.WriteLine("tempMin=" + tempMin);
            int tempID = -1;
            for (int i = 0; i < elevator.Length; i++)
                if (reDes[i] == tempMin)
                {
                    tempID = i;
                    break;
                }
            if (tempID == -1)
                return;
            System.Console.WriteLine("tempID=" + tempID);
            //将inputevent加入到分配的电梯的任务列表中
            this.eventIntoList(tempID, inputevent);
            //controller刷新电梯的当前任务
            this.updateTask(tempID, taskList[tempID].floors[0]);

            return;
        }
    }
}
